<template>
  <el-dialog
    :title="addOrEdit === 0 ? '新增企业' : '编辑企业'"
    :visible.sync="dialogFormVisible"
    center
    class="my-dialog"
    width="603px"
    top="10vh"
    @closed="closedHandler"
  >
    <el-form :model="form" :rules="rules" ref="comForm">
      <el-form-item label="企业编号" prop="eid" :label-width="formLabelWidth">
        <el-input v-model="form.eid" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="企业名称" prop="name" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        label="企业简称"
        prop="short_name"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.short_name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="企业简介" prop="intro" :label-width="formLabelWidth">
        <el-input
          v-model="form.intro"
          type="textarea"
          :rows="2"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="企业备注"
        prop="remark"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="removeForm">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入发起请求
import { addCompany, editCompany } from '@/api/company'
export default {
  name: 'comDialog',
  data () {
    return {
      addOrEdit: 0,
      title: '新增',
      form: {
        name: '',
        eid: '',
        short_name: '',
        intro: '',
        remark: ''
      },
      rules: {
        eid: [{ required: true, message: '企业编号不能为空', trigger: 'blur' }],
        name: [
          { required: true, message: '企业名称不能为空', trigger: 'blur' }
        ],
        short_name: [
          { required: true, message: '企业简称不能为空', trigger: 'blur' }
        ],
        intro: [
          { required: true, message: '企业简介不能为空', trigger: 'blur' }
        ]
      },
      dialogFormVisible: false,
      formLabelWidth: '105px'
    }
  },
  methods: {
    closedHandler () {
      // for (const key in this.form) {
      //   this.form[key] = ''
      // }
      this.form = {
        name: '',
        eid: '',
        short_name: '',
        intro: '',
        remark: ''
      }
      this.$refs.comForm.resetFields()
    },
    submitForm () {
      // 新增的清空
      if (this.addOrEdit === 0) {
        this.$refs.comForm.validate(valid => {
          if (valid) {
            addCompany(this.form).then(res => {
              console.log(res)
              // 关闭弹框
              this.dialogFormVisible = false
              // 传达给父组件中重新渲染页面
              this.$emit('successComDialog')
              // 提示
              this.$message.success('新增成功')
              this.$refs.comForm.resetFields()
            })
          } else {
            this.$message.warning('格式有误')
            return false
          }
        })
      } else {
        // 否则就是编辑按钮事件
        this.$refs.comForm.validate(valid => {
          if (valid) {
            editCompany(this.form).then(res => {
              console.log(res)
              // 关闭弹框
              this.dialogFormVisible = false
              // 传达给父组件中重新渲染页面
              this.$emit('successComDialog')
              // 提示
              this.$message.success('编辑成功')
              this.$refs.comForm.resetFields()
            })
          } else {
            this.$message.warning('格式有误')
            return false
          }
        })
      }
    },
    removeForm () {
      this.dialogFormVisible = false
    }
  }
}
</script>

<style></style>
